package com.baizhiedu.rsm.controller;


import com.baizhiedu.rsm.entity.StudentMessage;
import com.baizhiedu.rsm.service.UserLoginService;
import org.apache.shiro.web.filter.mgt.DefaultFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.Map;

import static org.apache.shiro.web.filter.mgt.DefaultFilter.user;

/**
 * @author lby
 */
@Controller
@RequestMapping("user")
public class UserLoginController {
    @Autowired
    private UserLoginService userLoginService;


    @RequestMapping("regist")
    public String regist(StudentMessage studentMessage){
//        studentMessage.setClassid((Integer) session.getAttribute("classid"));
        System.out.println("--------------");
        System.out.println(studentMessage);
        Boolean regist = userLoginService.regist(studentMessage);
        if(regist){
            return "redirect:/loginstudent.html";
        }else {
            return "redirect:/StudentRegist.html";
        }
    }
    /**
     * 如果登录成功就跳转显示学员进度的页面
     * 存储用户至Session中 在下个页面根据学生的名字显示学生的进度信息
     * @param request
     * @return
     */
    @RequestMapping("login")
    public String login(String xsphone,String xspassword, HttpServletRequest request){
        if(xsphone != null && xspassword != null){
            Map loginMap = userLoginService.login(xsphone,xspassword);
            boolean login = (boolean)loginMap.get("login");
            StudentMessage user = (StudentMessage) loginMap.get("user");

            if (login){
                //如果登录成功就跳转显示学员进度的页面
                HttpSession session = request.getSession();
                session.setAttribute("user", user);
                return "redirect:/ShowStudentSchedule.html";
            }else {
                return "redirect:/loginstudent.html";
            }
        }
        return "redirect:/loginstudent.html";
    }

    /**
     * 获取用户名 显示在进度提交页面中
     * @param request
     * @return
     */
    @RequestMapping("getUserName")
    @ResponseBody
    public String getUserName(HttpServletRequest request){
        HttpSession session = request.getSession();
        StudentMessage user = (StudentMessage) session.getAttribute("user");
        String xsname = user.getName();
        return xsname;
    }
}
